package org.nfx.blurvibratetoggle;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.widget.RemoteViews;

public class VibrateToggleProvider extends AppWidgetProvider {
    private static final String TAG = "VibrateToggleProvider";

    private static final String INTENT_CLICK = "org.nfx.blurvibratetoggle.VibrateToggleProvider.Clicked";

    private static final ComponentName THIS_APPWIDGET = new ComponentName("org.nfx.blurvibratetoggle",
            "org.nfx.blurvibratetoggle.VibrateToggleProvider");

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        // Update each requested appWidgetId
        RemoteViews view = buildUpdate(context);

        for (int appWidgetId : appWidgetIds) {
            appWidgetManager.updateAppWidget(appWidgetId, view);
        }
    }

    static RemoteViews buildUpdate(Context context) {
        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
        views.setOnClickPendingIntent(R.id.image, getLaunchPendingIntent(context));
        if (isVibrate(context)) {
            views.setImageViewResource(R.id.image, R.drawable.mute);
        } else {
            views.setImageViewResource(R.id.image, R.drawable.unmute);
        }
        return views;
    }

    static boolean isVibrate(Context context) {
        AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);

        boolean isVibrate = false;

        switch (am.getRingerMode()) {
            case AudioManager.RINGER_MODE_VIBRATE:
                isVibrate = true;
                break;
        }

        return isVibrate;
    }

    /**
     * Creates PendingIntent to notify the widget of a button click.
     *
     * @param context
     * @return
     */
    private static PendingIntent getLaunchPendingIntent(Context context) {
        Intent intent = new Intent(context, VibrateToggleProvider.class);
        intent.setAction(INTENT_CLICK);
        return PendingIntent.getBroadcast(context, 0, intent, 0);
    }

    /**
     * Updates the widget when something changes, or when a button is pushed.
     *
     * @param context
     */
    public static void updateWidget(Context context) {
        RemoteViews views = buildUpdate(context);
        // Update specific list of appWidgetIds if given, otherwise default to all
        final AppWidgetManager gm = AppWidgetManager.getInstance(context);
        gm.updateAppWidget(THIS_APPWIDGET, views);
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        super.onReceive(context, intent);
        super.onReceive(context, intent);
        final String action = intent.getAction();

        if (AudioManager.RINGER_MODE_CHANGED_ACTION.equals(action)) {
            updateWidget(context);
        } else if (INTENT_CLICK.equals(action)) {
            toggleVibrate(context);
        }
    }

    private void toggleVibrate(Context context) {
        AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);

        switch (am.getRingerMode()) {
            case AudioManager.RINGER_MODE_VIBRATE:
                am.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
                break;
            default: 
                am.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
                break;
        }
    }
}
